package com.etc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.dao.QuestionDao;
import com.etc.entity.LayUiData;
import com.etc.entity.Question;
import com.etc.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

/**
 * 
 * @author Administrator
 *
 */
@Service
public class QuestionServiceImpl implements QuestionService {
	
	//
	@Autowired
	private QuestionDao questionDao;
	

	@Override
	public boolean addQuestion(Question question) {
		// TODO Auto-generated method stub
		return questionDao.insert(question)>0;
	}

	@Override
	public boolean updateQuestion(Question question) {
		// TODO Auto-generated method stub
		return questionDao.updateByPrimaryKey(question)>0;
	}

	@Override
	public boolean deleteQuestion(Integer qid) {
		// TODO Auto-generated method stub
		return questionDao.deleteByPrimaryKey(qid)>0;
	}
	
	@Override
	public List<Question> getQuestions() {
		// TODO Auto-generated method stub
		return questionDao.selectAll();
	}
	@Override
	public Question getQuestionById(Integer qid) {
		// TODO Auto-generated method stub
		return questionDao.selectByPrimaryKey(qid);
	}

	@Override
	public LayUiData<Question> queryQuestions(Integer page,Integer limit,String strlike) {
		// TODO Auto-generated method stub
		Example example = new Example(Question.class);
		example.createCriteria().andLike("title", strlike+"%").orLike("content", strlike+"%");
		questionDao.selectByExample(example);
		
		List<Question> list = questionDao.selectByExample(example);
		
		PageHelper.startPage(page, limit);
		PageInfo<Question> pageinfo = new PageInfo<Question>(list);

		LayUiData<Question> laydata = new LayUiData<Question>();
		laydata.setCode(0);
		laydata.setMsg("succ");
		
		laydata.setCount(pageinfo.getTotal());//
		laydata.setData(list);

		return laydata;
	}

}
